<template>
  <view v-if="goodsInfo.goods_name" class="goods-detail-container">
    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular>
      <swiper-item class="swiper-item" v-for="(item, i) in goodsInfo.pics" :key="i">
        <image :src="item.pics_big" @click="preview(i)"></image>
      </swiper-item>
    </swiper>
    <!-- 商品信息区域 -->
    <view class="goods-info-box">
      <!-- 商品价格 -->
      <view class="price">￥{{goodsInfo.goods_price}}</view>
      <!-- 信息主体区域 -->
      <view class="goods-info-body">
        <view class="goods-name">{{goodsInfo.goods_name}}</view>
        <!-- 收藏 -->
        <view class="favi">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <!-- 运费 -->
      <view class="yf">快递：免运费</view>
    </view>
    <!-- 商品详情信息 -->
    <rich-text :nodes="goodsInfo.goods_introduce"></rich-text>
    <!-- 商品导航组件区域 -->
    <view class="goods-nav">
      <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick" @buttonClick="buttonClick" />
    </view>

  </view>
</template>

<script>
  import {
    mapMutations,
    mapGetters
  } from 'vuex'
  export default {
    computed: {
      ...mapGetters('m_cart', ['total'])
    },
    watch: {
      // 此方法页面首次加载不会调用这个监听器
      /* total(newVal) {
        const cartItem = this.options.find(item => item.text === '购物车')
        if(cartItem) {
          cartItem.info = newVal
        }
      } */
      // 修改
      total: {
        // handler 属性用来定义监听器的 function 处理函数
        handler(newVal) {
          const cartItem = this.options.find(item => item.text === '购物车')
          if (cartItem) {
            cartItem.info = newVal
          }
        },
        // immediate 属性用来声明此监听器，是否在页面初次加载完毕后调用
        immediate: true
      }
    },
    data() {
      return {
        goodsInfo: {},
        goods_id: 0,
        // 左侧按钮组配置对象
        options: [{
          icon: 'shop',
          text: '店铺',
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        // 右侧按钮组配置对象
        buttonGroup: [{
          text: '加入购物车',
          backgroundColor: '#ff0000',
          color: '#fff'
        }, {
          text: '立即购买',
          backgroundColor: '#ffa200',
          color: '#fff'
        }]
      };
    },
    onLoad(options) {
      this.goods_id = options.goods_id
      this.getGoodsDetail()
    },
    methods: {
      ...mapMutations('m_cart', ['addToCart']),
      async getGoodsDetail() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/detail?goods_id=' + this.goods_id)
        if (res.meta.status !== 200) return uni.$showMsg()
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display:block;" ').replace(
          /webp/g, 'jpg')
        this.goodsInfo = res.message
      },
      // 轮播图片预览
      preview(idx) {
        // 调用uni.previewImage() 方法预览图片
        uni.previewImage({
          // 当前放大图片的索引
          current: idx,
          // 要放大显示的所有图片url数组
          urls: this.goodsInfo.pics.map(imgs => imgs.pics_big)
        })
      },
      // 左侧按钮点击事件
      onClick(e) {
        // console.log(e)
        if (e.content.text === '购物车') {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },
      // 右侧按钮点击事件
      buttonClick(e) {
        // console.log(e)
        if (e.content.text === '加入购物车') {
          const goods = {
            goods_id: this.goodsInfo.goods_id, // 商品的id
            goods_name: this.goodsInfo.goods_name, // 商品的名称
            goods_price: this.goodsInfo.goods_price, // 商品的单价
            goods_count: 1, // 商品的数量
            goods_small_logo: this.goodsInfo.goods_small_logo, // 商品的图片
            goods_state: true // 商品的勾选状态
          }
          this.addToCart(goods)
        }
      }
    }
  }
</script>

<style lang="scss">
  .goods-detail-container {
    padding-bottom: 160rpx;
  }

  swiper {
    height: 750rpx;

    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }

  .goods-info-box {
    padding: 10px;
    padding-right: 0;

    .price {
      color: #C00000;
      font-size: 36rpx;
      margin: 10px 0;
    }

    .goods-info-body {
      display: flex;
      justify-content: space-between;

      .goods-name {
        font-size: 26rpx;
        padding-right: 10px;
      }

      .favi {
        width: 120px;
        font-size: 24rpx;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        border-left: 1px solid #efefef;
        color: gray;
      }
    }

    .yf {
      margin: 10px 0;
      font-size: 24rpx;
      color: gray;
    }
  }

  .goods-nav {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 160rpx;
    background-color: #FFFFFF;
  }
</style>
